#
# Makefile for gracket
#

PACKAGE = racket

srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
bindir = @bindir@
libdir = @libdir@
libpltdir = @libpltdir@
libpltdir_rel = @libpltdir_rel@
etcpltdir = @etcpltdir@
sharepltdir = @sharepltdir@
collectsdir = @collectsdir@
builddir = @builddir@

# for version.mak:
mainsrcdir = @srcdir@/../..
@INCLUDEDEP@ @srcdir@/../../version/version.mak

WXDIR = $(srcdir)/../wxxt

PLTSRCDIR = $(srcdir)/../..
BCBUILDDIR = $(builddir)/..

NOOP = :

# See ../Makefile about RUN_RACKET_<X>, which 
# typically redirects to RUN_THIS_RACKET_<X>:
RUN_THIS_RACKET_CGC = ../racket@CGC@
RUN_THIS_RACKET_MMM = ../racket@MMM@

###########################
#      Common Macros      #
###########################
CC = @CC@
LINKER = $(CC)
RANLIB = @RANLIB@
AR = @AR@
ARFLAGS = @ARFLAGS@
OPTIONS = @OPTIONS@ @MROPTIONS@
XLIB = @X_LIBS@
LDLIBS = @LIBS@
GCDIRECTORY = @GCDIR@
WBUILD = @WBUILD@
WXVARIANT =  @WXVARIANT@

NOGCINC = $(WXINC) -I$(BCBUILDDIR)/ -I$(PLTSRCDIR)/bc/include/ -I$(PLTSRCDIR)/version/
INC = -I$(PLTSRCDIR)/bc/$(GCDIRECTORY) $(NOGCINC)
CFLAGS = @COMPFLAGS@ @PROFFLAGS@ @CFLAGS@
CPPFLAGS = @PREFLAGS@
ALL_CFLAGS = $(INC) $(OPTIONS) -D$(WXVARIANT) $(CFLAGS) $(CPPFLAGS)
LDFLAGS = @LDFLAGS@
ARLIBFLAGS = $(LDFLAGS) $(LDLIBS)
###########################

STRIP_SIGNATURE = @STRIP_SIGNATURE@
RESTORE_SIGNATURE = @RESTORE_SIGNATURE@ "${srcdir}/../../mac/entitlements.plist"

STRIP_SIGNATURE_VIA_RACKET = $(BOOTSTRAP_RACKET) "${srcdir}/../../mac/codesign.rkt" --remove-signature
RESTORE_SIGNATURE_VIA_RACKET = $(BOOTSTRAP_RACKET) "${srcdir}/../../mac/codesign.rkt" -s - --entitlements

INSTALL_NAME_TOOL_VIA_RACKET = $(BOOTSTRAP_RACKET) "${srcdir}/../../mac/install_name_tool.rkt"

GRACKETLINKER = @MZLINKER@

LIBRKTIO = ../rktio/librktio.@LTA@
LIBRKTIO_a = $(LIBRKTIO)
LIBRKTIO_la = 

GRACKETLDLIBS@NOT_MINGW@ = ../libracket.@LIBSFX@ ../libmzgc.@LIBSFX@ $(LIBRKTIO_@LIBSFX@) $(LDLIBS)
GRACKETLDLIBS@MINGW@ = ../libracket.dll.a ../libmzgc.dll.a $(LDLIBS) -ldelayimp -static-libgcc
GRACKETLDFLAGS = $(LDFLAGS) -L..

GRACKETRES@NOT_MINGW@ =
GRACKETRESDEP@NOT_MINGW@ =
GRACKETRES@MINGW@ = -mwindows gres.o ../MemoryModule.@LTO@
GRACKETRESDEP@MINGW@ = gres.o ../MemoryModule.@LTO@

LOCALFLAGS_wx_xt = @WX_MMD_FLAG@
LOCALFLAGS_wx_mac = -I$(srcdir)/../../mac/racket -MMD -DWX_CARBON
LOCALFLAGS = $(LOCALFLAGS_@WXVARIANT@)

GRACKETOBJECTS = grmain.@LTO@

RACKETDEPS = ../libracket.@LIBSFX@ ../libmzgc.@LIBSFX@

MRSTATIC = 
MRSTATIC_STUB =

MZEXTRAEXPORT='GCDIR=$(GCDIRECTORY)'

LINKRESULT_wx_xt = gracket@CGC@@EXE_SUFFIX@
LINKRESULT_wx_mac = GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@
LINKRESULT = $(LINKRESULT_@WXVARIANT@)

SETUP_BOOT = -O "info@compiler/cm" -l- setup @BOOT_MODE@ $(srcdir)/../../setup-go.rkt ../../compiled
SETUP_BOOT_COLLECTS_PATH = $(SETUP_BOOT) collpath.inc collpath.d "$(srcdir)/../../start/collects-path.rkt" "${srcdir}/../.."

BOOTSTRAP_RACKET = @RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) $(SETUP_BOOT) ignored ignored.d

# Incremented each time the binaries change:
DOWNLOAD_BIN_VERSION = 1

bin:
	$(MAKE) @MAIN_VARIANT@

3m:
	$(MAKE) resources
	cd gc2; $(MAKE) 3m

cgc:
	$(MAKE) $(LINKRESULT)

both:
	$(MAKE) cgc
	$(MAKE) 3m

gracket@CGC@ : $(RACKETDEPS) $(LIBRKTIO) grmain.@LTO@ $(GRACKETOBJECTS) $(MRSTATIC_STUB) $(GRACKETRESDEP)
	$(GRACKETLINKER) $(GRACKETLDFLAGS) $(MRSTATIC) -o gracket@CGC@ $(GRACKETOBJECTS) $(GRACKETRES) $(GRACKETLDLIBS) $(MRSTATIC_STUB)
	@POST_LINKER@ gracket@CGC@

gres.o : $(srcdir)/../../worksp/gracket/gracket.rc
	@WINDRES@ -i $(srcdir)/../../worksp/gracket/gracket.rc -o gres.o

resources@NOT_MINGW@ :
	$(NOOP)

resources@MINGW@ : gres.o
	$(NOOP)

MRFW = Racket.framework/Versions/$(FWVERSION)/Racket
MRAPPSKEL = GRacket@CGC@.app/Contents/Info.plist

# Depending on MACLIBRKT_LINK_MODE, use Framework or statically link the framework's code:
MACLIBRKT_LINK_fw = -F.. -framework Racket
MACLIBRKT_LINK_static = ../libracket.@LIBSFX@ ../libmzgc.@LIBSFX@ $(LIBRKTIO) $(LDLIBS)

GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@: $(MZFW) $(MRAPPSKEL) grmain.@LTO@ 
	$(GRACKETLINKER) $(GRACKETLDFLAGS) -o GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@ grmain.@LTO@ -Wl,-headerpad_max_install_names $(MACLIBRKT_LINK_@MACLIBRKT_LINK_MODE@) -framework CoreFoundation @PROFFLAGS@
	@INSTALL_NAME_TOOL@ -change "Racket.framework/Versions/$(FWVERSION)/Racket" "@executable_path/../../../../Racket.framework/Versions/$(FWVERSION)/Racket" GRacket@CGC@.app/Contents/MacOS/GRacket@CGC@

$(MRAPPSKEL): $(srcdir)/../../mac/osx_appl.rkt $(srcdir)/../../version/racket_version.h $(srcdir)/../../mac/icon/GRacket.icns
	env BUILDBASE=../.. @RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) $(SETUP_BOOT) $(MRAPPSKEL) mrappskel.d $(srcdir)/../../mac/osx_appl.rkt $(srcdir)/../.. "@CGC@"
@INCLUDEDEP@ mrappskel.d

ee-app: gracket grmain_ee.@LTO@
	if [ "$(EEAPP)" = '' ] ; then echo "ERROR: You must specify EEAPP" ; else $(GRACKETLINKER) $(GRACKETLDFLAGS) $(MRSTATIC) -o $(EEAPP) grmain_ee.@LTO@ $(EEOBJECTS) $(GRACKETLDLIBS) $(MRSTATIC_STUB) ; fi

sunos4-static: dl_stub.@LTO@
	$(MAKE) gracket MRSTATIC=-static  MRSTATIC_STUB='dl_stub.@LTO@' LDNONSTATICLIBS=''

solaris-static:
	$(MAKE) gracket MRSTATIC="-Wl,-Bstatic" LDNONSTATICLIBS='-Wl,-Bdynamic -ldl -Wl,-Bstatic'

linux-static:
	$(MAKE) gracket MRSTATIC=--static LDNONSTATICLIBS='-ldl'

DEF_COLLECTS_DIR = -DINITIAL_COLLECTS_DIRECTORY='"'"`cd $(srcdir)/../../../collects; @PWD@`"'"'
DEF_CONFIG_DIR = -DINITIAL_CONFIG_DIRECTORY='"'"`cd $(srcdir)/../../..; @PWD@`/etc"'"'
DEF_C_DIRS = $(DEF_COLLECTS_DIR) $(DEF_CONFIG_DIR)

grmain.@LTO@ :  $(srcdir)/grmain.c $(srcdir)/../main.c \
            $(srcdir)/../cmdline.inc $(srcdir)/../src/stypes.h \
            $(srcdir)/../include/scheme.h
	$(CC) $(LOCALFLAGS) $(ALL_CFLAGS) $(DEF_C_DIRS) -c $(srcdir)/grmain.c -o grmain.@LTO@

grmain_ee.@LTO@ : gracket.@LTO@
	$(CC) $(LOCALFLAGS) $(ALL_CFLAGS) -DSTANDALONE_WITH_EMBEDDED_EXTENSION $(DEF_C_DIRS) -c $(srcdir)/grmain.cxx -o grmain_ee.@LTO@

ee-main:
	$(MAKE) grmain_ee.@LTO@

clean:
	rm -f *.@LTO@ *.d core gracket gracket3m
	rm -f gc2/*.@LTO@ gc2/xsrc/* gc2/macxsrc/* gc2/*.d gc2/*.dd

ICP=@ICP@
BUILDINFO="$(DESTDIR)$(libpltdir)/buildinfo"

# Prefix might be relative to srcdir, or it might be absolute, so we
# have to go up and install things from there.

install:
	$(MAKE) install-@MAIN_VARIANT@

install-3m:
	$(MAKE) install-@WXVARIANT@
	$(MAKE) install-@WXVARIANT@-3m
	$(MAKE) install-@WXVARIANT@-3m-final

install-cgc:
	$(MAKE) install-@WXVARIANT@
	$(MAKE) install-@WXVARIANT@-cgc
	$(MAKE) install-@WXVARIANT@-cgc-final

install-both:
	$(MAKE) install-@WXVARIANT@
	$(MAKE) install-@WXVARIANT@-3m
	$(MAKE) install-@WXVARIANT@-cgc
	$(MAKE) install-@WXVARIANT@-@MAIN_VARIANT@-final

install-post-collects:
	$(MAKE) install-@WXVARIANT@-post-collects

install-common:
	$(NOOP)

INSTALL_SETUP_BOOT = $(SETUP_BOOT) dummy_install dummy_install.d

# X11 ----------------------------------------

install-wx_xt:
	$(MAKE) install-common
	cd ..; rm -f "$(DESTDIR)$(libpltdir)/gracket@CGC_INSTALLED@"
	cd ..; rm -f "$(DESTDIR)$(libpltdir)/gracket@MMM_INSTALLED@"
	cd ..; echo 'MROPTIONS=@MROPTIONS@' >> $(BUILDINFO)
	cd ..; echo "MRLIBS=$(GUILIBS_@WXVARIANT@)" >> $(BUILDINFO)
	cd ..; echo "MRLDFLAGS=$(GRACKETLDFLAGS)" >> $(BUILDINFO)
	cd ..; mkdir -p "$(DESTDIR)$(libdir)";

install-no-lib-cgc-wx_xt:
	echo "no dynamic libs"

install-lib-cgc-wx_xt:
	$(NOOP)

GRACKET_NAME@NOT_MINGW@ = gracket
GRACKET_NAME@MINGW@ = GRacket

install-wx_xt-cgc:
	$(MAKE) @MRLIBINSTALL@-cgc-wx_xt
	cd ..; $(ICP) gracket/gracket@CGC@@EXE_SUFFIX@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"
	$(STRIP_SIGNATURE) "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"
	cd ..; @STRIP_DEBUG@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"
	@RUN_RACKET_CGC@ $(SETUP_BOOT_COLLECTS_PATH) @DIRCVTPRE@"$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"@DIRCVTPOST@ @GUI_COLLECTS_PATH@ @GUI_CONFIG_PATH@
	$(RESTORE_SIGNATURE) "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@CGC_INSTALLED@@EXE_SUFFIX@"

install-wx_xt-cgc-final:
	$(NOOP)

install-no-lib-3m-wx_xt:
	echo "no dynamic libs"

install-lib-3m-wx_xt:
	$(NOOP)

install-wx_xt-3m:
	$(MAKE) @MRLIBINSTALL@-3m-wx_xt
	cd ..; $(ICP) gracket/gracket@MMM@@EXE_SUFFIX@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"
	$(STRIP_SIGNATURE) "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"
	cd ..; @STRIP_DEBUG@ "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"
	@RUN_RACKET_MMM@ $(SETUP_BOOT_COLLECTS_PATH) @DIRCVTPRE@"$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"@DIRCVTPOST@ @GUI_COLLECTS_PATH@ @GUI_CONFIG_PATH@
	$(RESTORE_SIGNATURE) "$(DESTDIR)$(libpltdir)/$(GRACKET_NAME)@MMM_INSTALLED@@EXE_SUFFIX@"

install-wx_xt-3m-final:
	$(NOOP)

install-wx_xt-post-collects:
	$(NOOP)

# Mac OS X ----------------------------------------

FRAMEWORK_REL_PREFIX="@executable_path/../../../"
FRAMEWORK_ABS_PREFIX = "$(libpltdir)/"

install-wx_mac:
	$(MAKE) install-common
	cd ..; rm -rf $(DESTDIR)"$(prefix)/lib/GRacket@CGC_CAP_INSTALLED@.app"
	cd ..; rm -rf $(DESTDIR)"$(prefix)/lib/GRacket@MMM_CAP_INSTALLED@.app"

install-wx_mac-cgc:
	cd ..; $(ICP) -r gracket/GRacket@CGC@.app $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app"
	@RUN_RACKET_CGC@ $(SELF_RACKET_FLAGS) $(INSTALL_SETUP_BOOT) "$(srcdir)/../../mac/rename-app.rkt" $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app" "GRacket@CGC@" "GRacket@CGC_CAP_INSTALLED@"
	@INSTALL_NAME_TOOL@ -change "@executable_path/../../../../Racket.framework/Versions/$(FWVERSION)/Racket" "@FRAMEWORK_PREFIX@Racket.framework/Versions/$(FWVERSION)/Racket" $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@"
	@RUN_RACKET_CGC@ $(SETUP_BOOT_COLLECTS_PATH) $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@" @GR_APP_COLLECTS_PATH@ @GR_APP_CONFIG_PATH@
	@STRIP_DEBUG@ $(DESTDIR)"$(libpltdir)/GRacket@CGC_CAP_INSTALLED@.app/Contents/MacOS/GRacket@CGC_CAP_INSTALLED@"

install-wx_mac-cgc-final:
	$(NOOP)

install-wx_mac-3m:
	cd ..; $(ICP) -r "gracket/GRacket@MMM@.app" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app"
	@RUN_RACKET_MMM@ $(SELF_RACKET_FLAGS) $(INSTALL_SETUP_BOOT) "$(srcdir)/../../mac/rename-app.rkt" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app" "GRacket@MMM@" "GRacket@MMM_CAP_INSTALLED@"
	@INSTALL_NAME_TOOL@ -change "@executable_path/../../../../Racket.framework/Versions/$(FWVERSION)_3m/Racket" "@FRAMEWORK_PREFIX@Racket.framework/Versions/$(FWVERSION)_3m/Racket" $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@"
	@RUN_RACKET_MMM@ $(SETUP_BOOT_COLLECTS_PATH) $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@" @GR_APP_COLLECTS_PATH@ @GR_APP_CONFIG_PATH@
	@STRIP_DEBUG@ $(DESTDIR)"$(libpltdir)/GRacket@MMM_CAP_INSTALLED@.app/Contents/MacOS/GRacket@MMM_CAP_INSTALLED@"

install-wx_mac-3m-final:
	$(NOOP)

install-wx_mac-post-collects:
	cd ..; rm -rf $(DESTDIR)$(libpltdir)/Starter.app
	cd ..; $(ICP) -r gracket/Starter.app $(DESTDIR)$(libpltdir)/.


@INCLUDEDEP@ grmain.d
